package com.ts.manage.course.service;

import com.ts.framework.domain.course.CourseBase;
import com.ts.framework.domain.course.Teachplan;
import com.ts.framework.domain.course.TeachplanMedia;
import com.ts.framework.domain.course.TeachplanMediaPub;
import com.ts.framework.domain.course.ext.TeachplanNode;
import com.ts.framework.exception.ExceptionCast;
import com.ts.framework.model.response.CommonCode;
import com.ts.framework.model.response.ResponseResult;
import com.ts.manage.course.dao.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class TeachPlanService {
    @Autowired
    TeachplanMapper teachplanMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    CourseBaseRepository courseBaseRepository;
    @Autowired
    TeachplanRepository teachplanRepository;
    @Autowired
    TeachplanMediaRepository teachplanMediaRepository;
    @Autowired
    TeachplanMediaPubRepository teachplanMediaPubRepository;
    //查询课程计划
    public TeachplanNode findTeachPlanByCourseId(String courseId) {
        TeachplanNode teachplanNode = teachplanMapper.selectAllPlan(courseId);
        return teachplanNode;

    }

    //添加课程计划
    @Transactional
    public ResponseResult addTeachPlan(Teachplan teachplan) {
        //参数校验
        if (teachplan == null
                || StringUtils.isEmpty(teachplan.getCourseid())
                || StringUtils.isEmpty(teachplan.getPname())) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        //获取课程id
        String courseid = teachplan.getCourseid();
        //获取父节点id
        String parentid = teachplan.getParentid();
        if (StringUtils.isEmpty(parentid)) {
            //若为空则获取根节点
            parentid = getTeachPlanRoot(courseid);
        }
        //获取父节点信息
        Optional<Teachplan> optional = teachplanRepository.findById(parentid);
        if (!optional.isPresent()) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        //父节点
        Teachplan teachplanParent = optional.get();
        //父结点级别
        String parentGrade = teachplanParent.getGrade();
        //设置父节点
        teachplan.setParentid(parentid);
        teachplan.setStatus("0");//未发布
        //根据父结点来判断子结点的级别
        if (parentGrade.equals("1")) {
            teachplan.setGrade("2");
        } else if (parentGrade.equals("2")) {
            teachplan.setGrade("3");
        }
        //设置课程id
        teachplan.setCourseid(teachplanParent.getCourseid());
        teachplanRepository.save(teachplan);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //查询课程根结点
    public String getTeachPlanRoot(String courseId) {
        //校验课程id
        Optional<CourseBase> optional = courseBaseRepository.findById(courseId);
        if (!optional.isPresent()) {
            return null;
        }
        CourseBase courseBase = optional.get();
        //获取课程计划根节点
        List<Teachplan> teachplanList = teachplanRepository.findByCourseidAndParentid(courseId, "0");
        if (teachplanList == null || teachplanList.size() == 0) {
            //新增根节点
            Teachplan teachplanRoot = new Teachplan();
            teachplanRoot.setCourseid(courseId);
            teachplanRoot.setPname(courseBase.getName());
            teachplanRoot.setParentid("0");
            teachplanRoot.setGrade("1");//1级
            teachplanRoot.setStatus("0");//未发布
            teachplanRepository.save(teachplanRoot);
            return teachplanRoot.getId();
        }
        Teachplan teachplan = teachplanList.get(0);
        return teachplan.getId();
    }

    //删除课程计划
    public ResponseResult delTeachplan(String courseId) {
        teachplanRepository.deleteById(courseId);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //保存课程计划与视频的关联关系
    @Transactional
    public ResponseResult saveTeachplanMedia(TeachplanMedia teachplanMedia) {
        String teachplanId = teachplanMedia.getTeachplanId();
        //只允许给三级课程计划关联视频
        Optional<Teachplan> optional = teachplanRepository.findById(teachplanId);
        if (!optional.isPresent()) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        Teachplan teachplan = optional.get();
        String grade = teachplan.getGrade();
        if (!"3".equals(grade)) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        teachplanMediaRepository.save(teachplanMedia);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //保存课程计划媒资信息
    @Transactional
    public void saveTeachplanMediaPub(String courseId){
        //根据课程id从TeachplanMedia获取列表信息
        List<TeachplanMedia> tmList = teachplanMediaRepository.findTeachplanMediaByCourseId(courseId);
        List<TeachplanMediaPub> list = new ArrayList<>();
        for (TeachplanMedia tm:tmList) {
            TeachplanMediaPub teachplanMediaPub = new TeachplanMediaPub();
            BeanUtils.copyProperties(tm,teachplanMediaPub);
            list.add(teachplanMediaPub);
        }
        //从TeachplanMedia获取列表信息保存到TeachplanMediaPub表中
        teachplanMediaPubRepository.saveAll(list);
    }


}
